package com.langsin.zhaohairui;

import cn.hutool.core.util.IdUtil;
import com.langsin.bean.*;
import com.langsin.loujianguo.mapper.GoodsMapper;
import com.langsin.loujianguo.mapper.TeacherMapper;
import com.langsin.loujianguo.mapper.UserMapper;
import com.langsin.loujianguo.mapper.WorkerMapper;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.io.InputStream;
import java.util.HashMap;
import java.util.List;

/**
 * @author LQWQK
 * @version 1.0
 * @since 2022-06-25 18:37:27
 */
public class MyTest {

    SqlSession sqlSession = null;


    @Test
    public void testVO() throws Exception{

        QueryVo vo = new QueryVo();
        vo.setRoleCode("manager");
        vo.setAddress("济南浪曦");

        UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
        List<User> users = userMapper.queryByRoleAndAddress(vo);

        users.forEach(System.out::println);
    }


    @Test
    public void testMap() throws Exception{
        HashMap<String,Object> map = new HashMap<>();
        map.put("roleCode", "plain");
        map.put("address", "济南浪曦");

        UserMapper userMapper = sqlSession.getMapper(UserMapper.class);

        List<User> users = userMapper.queryUserByMap(map);

        users.forEach(System.out::println);
    }


    @Test
    public void testTea() throws Exception{
        Integer tid = 1009;
        TeacherMapper teacherMapper = sqlSession.getMapper(TeacherMapper.class);
        Teacher teacher = teacherMapper.queryByID(tid);
        System.out.println(teacher);

    }

    @Test
    public void testUser() throws Exception{
        Integer uid = 702;
        UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
        User user = userMapper.queryById(uid);
        System.out.println(user);
    }

    @Test
    public void testTabname() throws Exception {
        String tabName = "goods";
        GoodsMapper goodsMapper = sqlSession.getMapper(GoodsMapper.class);
        int counts = goodsMapper.counts(tabName);
        System.out.println(tabName + "=" + counts);
    }

    @Test
    public void testMapperLike2() throws Exception {
        String name = "X";
        GoodsMapper goodsMapper = sqlSession.getMapper(GoodsMapper.class);

        List<Goods> goods = goodsMapper.queryByName2(name);
        for (Goods good : goods) {
            System.out.println(good);
        }
    }

    @Test
    public void testMapperLike() throws Exception {
        String name = "%C%";
        GoodsMapper goodsMapper = sqlSession.getMapper(GoodsMapper.class);
        List<Goods> goods = goodsMapper.queryByName(name);
        for (Goods good : goods) {
            System.out.println(good);
        }
    }

    @Test
    public void testMapperGoods() throws Exception {
        Goods goods = new Goods();
        goods.setGoodsName("品胜 typeC数据线");
        goods.setShopPrice(15.88);

        GoodsMapper goodsMapper = sqlSession.getMapper(GoodsMapper.class);

        int i = goodsMapper.addGoods(goods);

        System.out.println(goods);
    }


    @Test
    public void testMapper3() throws Exception {
        Worker worker = new Worker();
        worker.setWname("警察");
        worker.setAge(30);
        worker.setSex("女");

        WorkerMapper workerMapper = sqlSession.getMapper(WorkerMapper.class);
        int i = workerMapper.insertWorker2(worker);
        System.out.println(i + "------->" + worker);
    }


    @Test
    public void testMapper2() throws Exception {
        Worker worker = new Worker();
        // 主键 整数自增, 字符串uuid , 雪花算法(课下自己了解)
        worker.setWid(IdUtil.getSnowflakeNextIdStr());
        worker.setWname("律师");
        worker.setAge(40);
        worker.setSex("女");

        WorkerMapper workerMapper = sqlSession.getMapper(WorkerMapper.class);
        int row = workerMapper.insertWorker(worker);
        System.out.println(row);
    }

    @Test
    public void testMapper1() throws Exception {
        // 获取mapper接口 (dao) 的 动态代理实现类.
        WorkerMapper workerMapper = sqlSession.getMapper(WorkerMapper.class);

        List<Worker> workers = workerMapper.selectByAge(50);

        workers.forEach(System.out::println);
    }


    // 每次单词测试之前 都会执行
    @Before
    public void before() throws Exception {
        InputStream stream = Resources.getResourceAsStream("mybatis.xml");
        SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
        SqlSessionFactory sqlSessionFactory = builder.build(stream);
        // 默认不会自动提交.
        sqlSession = sqlSessionFactory.openSession();
    }

    //每次单词测试之后 都会执行
    @After
    public void after() throws Exception {
        sqlSession.commit();
        sqlSession.close();
    }

    @Test
    public void testUpdate() throws Exception {
        // 演示一个 修改.
        Worker worker = new Worker();
        worker.setAge(500);
        worker.setWid("5af9f088-5e5d-11e9-9dc0-28d244d68b7c");
        int row = sqlSession.update("worker.updateAgeByID", worker);
        System.out.println(row);
    }
}
